package com.zj.sixtyDayChallenge.tree;

import java.util.Stack;

/**
 * @program: algorithm
 * @description: 二叉树的最大深度
 * 104 maximum-depth-of-binary-tree
 * //给定一个二叉树，找出其最大深度。
 * //
 * // 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
 * //
 * // 说明: 叶子节点是指没有子节点的节点。
 * //
 * // 示例： 给定二叉树 [3,9,20,null,null,15,7]，
 * //
 * //     3
 * //   / \
 * //  9  20
 * //    /  \
 * //   15   7
 * //
 * // 返回它的最大深度 3 。
 * //
 * // Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 1501 👎 0
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class MaximumDepthOfBinaryTree {
    public static void main(String[] args) {
        Solution solution = new MaximumDepthOfBinaryTree().new Solution();
        TreeNode treeNode = new TreeNode(1, new TreeNode(2, new TreeNode(4), null), new TreeNode(3, null, new TreeNode(5)));

        System.out.println(solution.maxDepth(treeNode));

    }
    //leetcode submit region begin(Prohibit modification and deletion)


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /**
     * [1,2,3,4,null,null,5]
     */
    class Solution {
//        public int maxDepth(TreeNode root) {
//
//            if (root == null) {
//                return 0;
//            }
//            int deep = 0;
//            Queue<TreeNode> stack = new LinkedList<>();
//            stack.offer(root);
//
//            while (!stack.isEmpty()) {
//                deep++;
//                int size = stack.size();
//                for (int i = 0; i < size; i++) {
//                    TreeNode temp = stack.poll();
//                    if (temp.left != null) {
//                        stack.offer(temp.left);
//                    }
//
//                    if (temp.right != null) {
//                        stack.offer(temp.right);
//                    }
//                }
//
//            }
//
//            return deep;
//        }


        /**
         * 必须用队列，好好想想，栈是不行的
         * @param root
         * @return
         */
        public int maxDepth(TreeNode root) {

            if (root == null) {
                return 0;
            }
            int deep = 0;
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);

            while (!stack.isEmpty()) {
                deep++;
                int size = stack.size();
                for (int i = 0; i < size; i++) {
                    TreeNode temp = stack.pop();
                    if (temp.left != null) {
                        stack.push(temp.left);
                    }

                    if (temp.right != null) {
                        stack.push(temp.right);
                    }
                }

            }

            return deep;
        }


    }
//leetcode submit region end(Prohibit modification and deletion)

}
